package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.constant.ConstantValue;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.UserGetLoginDto;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.service.IUserRegisterService;
import com.cskaoyan.user.utils.VerifyRegisterDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/25 16:39
 * @Description:
 * @PACKAGE_NAME： com.cskaoyan.user.controller
 */


@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    ILoginService loginService;

    @Autowired
    IKaptchaService kaptchaService;

    @Autowired
    IUserRegisterService userRegisterService;

    /**
     * 检查用户是否登录请求
     *
     * @param request 通过request 获取请求头中的user_info头 获取到数据
     * @return
     */
    @GetMapping("login")
    public ResponseData getUserLogin(HttpServletRequest request) {
        String header = request.getHeader(ConstantValue.TOKEN_USER_INFO);
        UserGetLoginDto userGetLoginDto = JSON.parseObject(header, UserGetLoginDto.class);
        return new ResponseUtil<>().setData(userGetLoginDto);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest   用户登录数据
     * @param httpServletRequest 在request中获取cookie中的验证码id 对验证码进行操作
     * @param response
     * @return
     */
    @PostMapping("login")
    public ResponseData userLogin(@RequestBody @NotNull UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest, HttpServletResponse response) {
        //对验证码进行验证 验证码不正确不进行密码严重
        verifyKaptcha(httpServletRequest, userLoginRequest.getCaptcha());

        UserLoginResponse login = null;
        try {
            login = loginService.login(userLoginRequest);
        } catch (BizException e) {
            e.printStackTrace();
            return new ResponseUtil<>().setErrorMsg(Integer.parseInt(e.getErrorCode()), e.getMessage());
        }
        //在返回头中增加access_token的token数据
        Cookie cookie = new Cookie(ConstantValue.LOGIN_TOKEN, login.getToken());
        cookie.setPath("/");
        CookieUtil.setCookie(response, cookie);

        return new ResponseUtil<>().setData(login);
    }

    /**
     * 用户 注册
     *
     * @param userRegisterRequest 用户注册数据
     * @param httpServletRequest  在request中获取cookie中的验证码id 对验证码进行操作
     * @return
     */
    @PostMapping("register")
    public ResponseData registerUser(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest httpServletRequest) {
        verifyKaptcha(httpServletRequest, userRegisterRequest.getCaptcha());
        VerifyRegisterDataUtils.verifyRegisterData(userRegisterRequest);
        userRegisterService.register(userRegisterRequest);
        return new ResponseUtil<>().setData(userRegisterRequest);
    }

    @GetMapping("loginOut")
    public ResponseData UserLoginOut(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        return new ResponseUtil<>().setData(null);
    }


    /**
     * 对验证码进行验证 传入验证码和httpRequest
     *
     * @param httpServletRequest
     * @param captcha
     */
    private void verifyKaptcha(HttpServletRequest httpServletRequest, String captcha) {
        KaptchaCodeRequest request = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(httpServletRequest, ConstantValue.KAPTCHA_UUID);
        request.setCode(captcha);
        request.setUuid(uuid);
        kaptchaService.validateKaptchaCode(request);
    }

    /**
     * 用户激活接口
     * @param userVerifyRequest
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 小焦
     * @since 2022/10/26 下午 03:51
     */
    @GetMapping("/verify")
    public ResponseData verify(UserVerifyRequest userVerifyRequest){
        userRegisterService.verify(userVerifyRequest);
        return new ResponseUtil<>().setData(null);
    }
}
